﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EpiPoker.BusinessManagementObjects
{
    /// <summary>
    /// Determine what hand you have
    /// </summary>
    public class Combinaison
    {
        #region combinaisons
        /// <summary>
        /// You have a Flush
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isFlush(List<Objects.Card> cards)
        {
            int nb = 0;

            for (int i = 0; i < (cards.Count - 1); i++)
            {
                if (nb == 4)
                    break;

                if ((int)cards[i].Value == ((int)cards[i + 1].Value - 1)
                    && (int)cards[i].Color == (int)cards[i + 1].Color)
                    ++nb;
                else
                    nb = 0;
            }

            if (nb >= 4)
                return true;
            else
                return false;
        }

        /// <summary>
        /// You have a Carre
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isCarre(List<Objects.Card> cards)
        {
            int i = 0;

            while (i < (cards.Count - 3))
            {
                if (((int)cards[i].Value == (int)cards[i + 1].Value)
                    && ((int)cards[i].Value == (int)cards[i + 2].Value)
                    && ((int)cards[i].Value == (int)cards[i + 3].Value))
                    return true;
                ++i;
            }

            return false;
        }

        /// <summary>
        /// You have a Full
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isFull(List<Objects.Card> cards)
        {
            if (nbBrelan(cards) == 1 && (nbPaires(cards) == 1
                || nbPaires(cards) == 2))
                return true;

            if (nbBrelan(cards) == 2)
                return true;

            return false;
        }

        /// <summary>
        /// You have a Flush
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isCouleur(List<Objects.Card> cards)
        {
            int nb;

            for (int i = 0; i < 4; i++)
            {
                nb = 0;

                for (int j = 0; j < cards.Count; j++)
                {
                    if (cards[j].Color == ((Objects.Color)i))
                        ++nb;
                }

                if (nb >= 5)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// You have a Quinte
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isQuinte(List<Objects.Card> cards)
        {
            int nb = 0;

            for (int i = 0; i < (cards.Count - 1); i++)
            {
                if (nb == 4)
                    break;

                if ((int)cards[i].Value == ((int)cards[i + 1].Value - 1))
                    ++nb;
                else
                    nb = 0;
            }

            if (nb >= 4)
                return true;
            else
                return false;
        }

        /// <summary>
        /// You have a Brelan
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isBrelan(List<Objects.Card> cards)
        {
            if (nbBrelan(cards) == 1)
                return true;

            return false;
        }

        /// <summary>
        /// You have a Double Pair
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isDoublePaire(List<Objects.Card> cards)
        {
            if (nbPaires(cards) > 1)
                return true;

            return false;
        }

        /// <summary>
        /// You have a Pair
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>True if it is, false if not</returns>
        public static Boolean isPaire(List<Objects.Card> cards)
        {
            if (nbPaires(cards) == 1)
                return true;

            return false;
        }

#endregion

        #region enable_combinaison_count

        /// <summary>
        /// How many pairs there are
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>How many pairs there are</returns>
        private static int nbPaires(List<Objects.Card> cards)
        {
            int nb = 0;
            int i = 0;

            while (i < (cards.Count - 2))
            {
                if (((int)cards[i].Value == (int)cards[i + 1].Value)
                    && ((int)cards[i].Value != (int)cards[i + 2].Value))
                {
                    i = i + 2;
                    ++nb;
                }
                else if (((int)cards[i].Value == (int)cards[i + 1].Value)
                    && ((int)cards[i].Value == (int)cards[i + 2].Value))
                    i = i + 3;
                else
                    ++i;
            }

            if (((int)cards[5].Value == (int)cards[6].Value)
                && ((int)cards[5].Value != (int)cards[4].Value))
                ++nb;

            return nb;
        }

        /// <summary>
        /// How many brelans there are
        /// </summary>
        /// <param name="cards">Your hand + Flop</param>
        /// <returns>How many brelans there are</returns>
        private static int nbBrelan(List<Objects.Card> cards)
        {
            int nb = 0;
            int i = 0;

            while (i < (cards.Count - 3))
            {
                if (((int)cards[i].Value == (int)cards[i + 1].Value)
                    && ((int)cards[i].Value == (int)cards[i + 2].Value)
                    && ((int)cards[i].Value != (int)cards[i + 3].Value))
                {
                    i = i + 3;
                    ++nb;
                }
                else if (((int)cards[i].Value == (int)cards[i + 1].Value)
                   && ((int)cards[i].Value == (int)cards[i + 2].Value)
                   && ((int)cards[i].Value == (int)cards[i + 3].Value))
                    i = i + 4;
                else
                    ++i;
            }

            if (((int)cards[4].Value == (int)cards[6].Value)
                && ((int)cards[4].Value == (int)cards[5].Value)
                && ((int)cards[4].Value != (int)cards[3].Value))
                ++nb;

            return nb;
        }
    }
    #endregion
}